(defun primep(n)
  (cond 
    ((< n 0) (primep (- n)))
    ((<= n 3) t)
    ((zerop (mod n 2)) nil)
    (t (loop 
	  for i = 3 then (+ 2 i) 
	  with lim = (sqrt n) 
	  while (<= i lim) 
	  when (zerop (mod n i)) do (return nil) 
	  finally (return t)))))

(defun digits(n)
  (sort (loop for i = n then (floor (/ i 10))
       while (> i 0)
       collect (mod i 10)) #'<))
 
(defun is-perm(a b)
  (equal (digits a) (digits b)))
  
(defvar *primes* 
  (loop for i from 1000 to 10000
     when (primep i)
     collect i))

(defun find-seq (a b)
  (let* ((na (car a)) 
	(nb (car b))
	(nc (+ nb (- nb na))))
    (cond ((null a) (print "end"))
	  ((null (cdr b)) (find-seq (cdr a) (cddr a)))
	  ((and (is-perm na nb)
		(primep nc)
		(is-perm na nc))
	   (format t "~d ~d ~d~%" na nb nc) (find-seq a (cdr b)))
	  (t (find-seq a (cdr b))))))